* =====HETEROGENEOUS EFFECTS ===================================================

/* 
This one is a bit tricky, since we want to transpose the column of coefficients
to rows instead.
*/

* INCOME, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_income"
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}


esttab using "${tables}tabE1inc_est.tex", ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* INCOME, summary statistics	
eststo clear	
forvalues i = 1/5 {
	estimates use "${ster}reg_hetero_bev_income"
	estadd scalar income = e(MeanHetero_`i')/1000 // measuring income in 1000,-
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1inc_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 income obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	"Household income (1000 NOK)" "Households per group" ///
	) fmt(2 2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* AGE, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_age"
*eststo
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}

esttab using "${tables}tabE1age_est.tex", ///
	///keep(bergen_commuters_post)  ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// 
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* AGE, summary statistics	
eststo clear	
forvalues i = 1/5 {
	estimates use "${ster}reg_hetero_bev_age"
	estadd scalar age = e(MeanHetero_`i')
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1age_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 age obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	"Average age" "Households per group" ///
	) fmt(2 2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* FAMILY STATUS, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_famstatus"
*eststo
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}

esttab using "${tables}tabE1_famstatus_est.tex", ///
	///keep(bergen_commuters_post)  ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* FAMILY STATUS, summary statistics	
eststo clear	
forvalues i = 1/4 {
	estimates use "${ster}reg_hetero_bev_famstatus"
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1_famstatus_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	/*"Family status"*/ "Households per group" ///
	) fmt(2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* EDUCATION, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_educ"
*eststo
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}

esttab using "${tables}tabE1_educ_est.tex", ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// 
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* EDUCATION, summary statistics	
eststo clear	
forvalues i = 1/5 {
	estimates use "${ster}reg_hetero_bev_educ"
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1_educ_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	/*"Education"*/ "Households per group" ///
	) fmt(2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* WORK DISTANCE, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_wd"
*eststo
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}

esttab using "${tables}tabE1_wd_est.tex", ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// 
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* WORK DISTANCE, summary statistics	
eststo clear	
forvalues i = 1/5 {
	estimates use "${ster}reg_hetero_bev_wd"
	estadd scalar wd = e(MeanHetero_`i')
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1_wd_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 wd obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	"Work distance (kilometers)" "Households per group" ///
	) fmt(2 2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* PUBLIC TRANSPORT DIFFERENCE, estimates
eststo drop *	
estimates use "${ster}reg_hetero_bev_ptdiff"
*eststo
esttab, keep(bergen_commuters_post_*) se nostar stats(N)

matrix C = r(coefs)
matrix list C
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0

foreach name of local rnames {
   local ++i
   local j 0
   capture matrix drop b
   capture matrix drop se
   foreach model of local models {
     local ++j
      matrix tmp = C[`i', 2*`j'-1]
      if tmp[1,1]<. {
          matrix colnames tmp = `model'
         matrix b = nullmat(b), tmp
         matrix tmp[1,1] = C[`i', 2*`j']
         matrix se = nullmat(se), tmp
      }
   }
   ereturn post b
   quietly estadd matrix se
   eststo `name'
}

esttab using "${tables}tabE1_ptdiff_est.tex", ///
	coeflabels( ///
	active "Post $\times$ Paying commuters $\times$ Bergen" /// 
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

* PUBLIC TRANSPORT DIFFERENCE, summary statistics	
eststo clear	
forvalues i = 1/5 {
	estimates use "${ster}reg_hetero_bev_ptdiff"
	estadd scalar ptdiff = e(MeanHetero_`i')
	estadd scalar obser = e(NHetero_`i')
	estadd scalar mv2014 = e(MeanValue1_`i')
	estadd scalar mv2017 = e(MeanValue2_`i')
	eststo temp`i'
}

esttab temp* ///
	using "${tables}tabE1_ptdiff_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(mv2014 mv2017 ptdiff obser, ///
	label("Mean depvar 2014" "Mean depvar 2017" ///
	"Time public transport minus time car (minutes)" "Households per group" ///
	) fmt(2 2 2 0) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)